#include "threadpool.h"
#include "server.h"
int userInit(char *path);

//int recvn(int netfd, void* buf, int length);
int work(int netfd);

void* threadFunc(void* arg);


int makeWorker(threadPool_t *pthreadPool){
    for(int i=0;i<pthreadPool->workerNum;i++){
        pthread_create(&pthreadPool->tidArr[i],NULL,threadFunc,pthreadPool);
    }

    return 0;
}
void* threadFunc(void* arg){
    threadPool_t* pthreadPool=(threadPool_t*)arg;
    while(1){
        pthread_mutex_lock(&pthreadPool->taskQueue.mutex);
        while(pthreadPool->taskQueue.queueSize<=0){
            pthread_cond_wait(&pthreadPool->taskQueue.cond,&pthreadPool->taskQueue.mutex);
        }
        int netfd=pthreadPool->taskQueue.head->netfd;
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);

        printf("tid = %lu\n",pthread_self());
        printf("child get a task!\n");
        
        serverWork(netfd);
        
        close(netfd);
        // 恢复空闲
        printf("1 worker finish a task!\n");
    }
}

